<template>
    <transition name="slide">
        <music-list :rank="rank" :title="title" :bgImage="bgImage" :songs="RankSongs"></music-list>
    </transition>
</template>

<script>
import MusicList from 'components/music-list/music-list'
import { mapGetters } from 'vuex'
import { getRankMusic } from 'api/rank'
import { ERR_OK } from 'api/config'
import { createSong } from 'common/js/song'

export default {
    data() {
        return {
            RankSongs: [],

            rank: true
        }
    },
    created() {
        this._getRankMusic()
    },
    components: {
        MusicList
    },
    computed: {
        title() {
            return this.topList.topTitle
        },
        bgImage() {
            return this.topList.picUrl
        },
        ...mapGetters([
            'topList'
        ])
    },
    methods: {
        _getRankMusic() {
            // 如果检测不到toplist.id执行返回到rank操作
            if (!this.topList.id) {
                this.$router.push('/rank')
                return
            }

            getRankMusic(this.topList.id).then((res) => {
                if (res.code === ERR_OK) {
                    this.RankSongs = this._normalizeSongs(res.songlist)
                }
            })
        },

        // 格式化音乐操作
        _normalizeSongs(list) {
            let ret = []
            list.forEach((item) => {
                const musicData = item.data
                if (musicData.songid && musicData.albumid) {
                    ret.push(createSong(musicData))
                }
            })
            return ret
        }
    }
}
</script>

<style lang="stylus" scoped>
    .slide-enter-active, .slide-leave-active
        transition: all 0.3s ease
    .slide-enter, .slide-leave-to
        transform: translate3d(100%, 0, 0)
</style>